package cn.virens.common.boot.provider.apijson.autoconfig;

import cn.virens.common.boot.provider.apijson.VirAPIJSONCreator;
import cn.virens.common.boot.provider.apijson.VirAPIJSONCreatorSimple;
import cn.virens.common.util.exception.APIException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Role;

import javax.sql.DataSource;

@AutoConfiguration
@Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
@EnableConfigurationProperties({ApijsonProperties.class})
@ConditionalOnProperty(prefix = "virens.apijson", value = "enabled")
public class ApijsonAutoConfig {

    @Bean(name = "mVirAPIJSONCreator")
    @ConditionalOnBean(value = {DataSource.class})
    public VirAPIJSONCreator mVirAPIJSONCreator(DataSource dataSource, ApijsonProperties properties) throws APIException {
        return new VirAPIJSONCreatorSimple(dataSource, properties);
    }
}
